import Vue from 'vue'
import VueRouter from 'vue-router'
import UserLogin from '@/components/UserLogin.vue'
import AdminLogin from '@/components/AdminLogin.vue'
import UserMain from '@/components/UserMain.vue'
import AdminMain from '@/components/AdminLogin.vue'
import shopMain from '@/components/shopMain.vue'
import shopFirst from '@/components/ShopsFirst.vue'
import Commodity from '@/components/CommodityList.vue'
import Shops  from "@/components/ShopsList.vue";
import updatePasswords from '@/components/updatePasswords.vue'
import XianHua from '@/components/XianHua.vue'
import TypesLists from '@/components/TypesList.vue'
import goShop from '@/components/goShop.vue'
import ordersLists from '@/components/ordersLists.vue'
import UserOrder from '@/components/UserOrder.vue'
import MidAutumnLoveFlowers from "@/components/mid-autumn-love-flowers.vue";
import BrandFlowers from "@/components/brand-flowers.vue";
import BirthdayFlowers from "@/components/birthday-flowers.vue";
import LoveFlowers from "@/components/love-flowers.vue";
import PingLun from "@/components/pinglun.vue";
import AaVXin from "@/components/AaVXin.vue";



Vue.use(VueRouter)

const routes = [
  {
    path: '/userLogin',
    name: 'userLogin',
    component: UserLogin
  },
  {
    path: '/adminLogin',
    name: 'adminLogin',
    component: AdminLogin
  },
  {
    path: '/',
    name: 'userMain',
    component: UserMain
  },
  {
    path: '/adminMain',
    name: 'adminMain',
    component: AdminMain,
    meta: { requiresAuth: true }, // 添加一个元信息，用于标识需要认证的路由
  },
  {//个人中心
    path: '/shopMain',
    name: 'shopMain',
    component: shopMain
  },
  {//个人中心
    path: '/shopFirst',
    name: 'shopFirst',
    component: shopFirst
  },
  {//个人中心
    path: '/Commodity',
    name: 'Commodity',
    component: Commodity
  },
  {//个人中心
    path: '/Shops',
    name: 'Shops',
    component: Shops
  },
  {//个人中心
    path: '/updatePasswords',
    name: 'updatePasswords',
    component: updatePasswords
  },
  {//个人中心
    path: '/XianHua',
    name: 'XianHua',
    component: XianHua
  },
  {
    path: '/TypesLists',
    name: 'TypesLists',
    component: TypesLists
  },
  {
    path: '/goShop',
    name: 'goShop',
    component: goShop
  },
  {
    path: '/ordersLists',
    name: 'ordersLists',
    component: ordersLists
  },{
    path: '/userOrder',
    name: 'userOrder',
    component: UserOrder
  },

//     轮播图跳转的页面
  {
    path: '/mid-autumn-love-flowers',
    name: 'MidAutumnLoveFlowers',
    component: MidAutumnLoveFlowers
  },
  {
    path: '/brand-flowers',
    name: 'BrandFlowers',
    component: BrandFlowers
  },
  {
    path: '/birthday-flowers',
    name: 'BirthdayFlowers',
    component: BirthdayFlowers
  },
  {
    path: '/love-flowers',
    name: 'LoveFlowers',
    component: LoveFlowers
  },
  {
    path: '/pinglun',
    name: 'pinglun',
    component: PingLun
  },
  {
    path: '/aavxin',
    name: 'aavxin',
    component: AaVXin
  }


]
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes,
  mode: 'history', // 使用 HTML5 History 模式
})
//
// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 检查目标路由是否需要认证
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 获取用户名
    const username = getCookie('username');
    alert(username)
    if (!username) {
      // 如果没有用户名，重定向到登录页面
      next('/'); // 假设 adminLogin 是登录页面的路径
    } else {
      // 如果有用户名，放行
      next();
    }
  } else {
    // 如果目标路由不需要认证，直接放行
    next();
  }
});

function getCookie(name) {
  let cookieArray = document.cookie.split(';');
  cookieArray = cookieArray.map(cookie => cookie.trim()); // 移除前后空格
  let cookie = cookieArray.find(row => row.startsWith(name + '='));
  if (cookie) {
    return decodeURIComponent(cookie.split('=')[1]); // 解码 cookie 值
  }
  return '';
}


export default router
